package com.icity.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.icity.common.Result;
import com.icity.entity.Menu;
import com.icity.entity.RoleUser;
import com.icity.entity.User;
import com.icity.entity.system.MenuListSupport;
import com.icity.entity.system.RoleMenu;
import com.icity.entity.system.TableSupport;
import com.icity.service.SystemService;
import com.icity.util.IdUtils;

@RestController
@RequestMapping(value = "/admin/system")
public class SystemController {

	@Autowired
	private SystemService systemService;

	@RequestMapping(value = "/update/pwd")
	@ResponseBody
	public Result updatePwd(HttpServletRequest request) {
		Result result = new Result();
		String phone = request.getParameter("id");
		String pwd = request.getParameter("password");
		System.out.println("pwd---"+pwd+"--phone---"+phone);
		try {
			int i = systemService.updatePwd(phone, pwd);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(i);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/menulist")
	@ResponseBody
	public Result getMenuListByUser(HttpServletRequest request) {
		String token =request.getParameter("token");
		Map<Object,Object> map = new HashMap<Object,Object>();
		Result result = new Result();
		try {
			map.put("role_code", token);
			List<MenuListSupport> list = systemService.getMenuListByUser(map);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(list);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/menutable")
	@ResponseBody
	public Result getMenuList(HttpServletRequest request) {
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String name = request.getParameter("name");
		String status = request.getParameter("status");
		Map<Object,Object> map = new HashMap<Object,Object>();
		if (!name.equals("undefined")) {
			map.put("name", name);
		}
		if (!status.equals("undefined")) {
			map.put("status", status);
		}
		Result result = new Result();
		try {
			map.put("role_code", 1);
			map.put("page", (page-1)*limit);
			map.put("limit", limit);
			TableSupport<Menu> list = (TableSupport<Menu>) systemService.getMenuLsit(map);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(list);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/usertable")
	@ResponseBody
	public Result getUserList(HttpServletRequest request) {
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String name = request.getParameter("name");
		String is_use = request.getParameter("status");
		String phone = request.getParameter("phone");
		Map<Object,Object> map = new HashMap<Object,Object>();
		if (!name.equals("undefined")) {
			map.put("name", name);
		}
		if (!is_use.equals("undefined")) {
			map.put("is_use", is_use);
		}
		if (!phone.equals("undefined")) {
			map.put("phone", phone);
		}
		Result result = new Result();
		try {
			map.put("role_code", 1);
			map.put("page", (page-1)*limit);
			map.put("limit", limit);
			TableSupport<User> list = (TableSupport<User>) systemService.getUserList(map);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(list);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/menu/parent")
	@ResponseBody
	public Result getParentMenu(HttpServletRequest request) {
		Result result = new Result();
		try {
			List<Map<Object,Object>> data = systemService.getParentMen();
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(data);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/user/parent")
	@ResponseBody
	public Result getParentUser(HttpServletRequest request) {
		Result result = new Result();
		try {
			List<Map<Object,Object>> data = systemService.getParentUser();
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(data);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}


	@RequestMapping(value = "/menu/delete")
	@ResponseBody
	public Result deleteMenu(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("ids"));
		Result result = new Result();
		try {
			int data = systemService.deleteMenuById(id);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(data);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/user/delete")
	@ResponseBody
	public Result deleteUser(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("ids"));
		Result result = new Result();
		try {
			int data = systemService.deleteUserById(id);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(data);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@PostMapping(value = "/menu/save")
	@ResponseBody
	public Result saveMenu(HttpServletRequest request,Menu menu) {
		System.out.println("parent_code"+menu.getParent_code());
		menu.setUnique_code(IdUtils.uuid());
		menu.setCreate_time(new Date());
		menu.setUpdate_time(new Date());
		Result result = new Result();
		try {
			boolean flag = systemService.isExitMenuByName(menu.getId());
			int data = 0;
			if(flag==true){
				data = systemService.updateMenu(menu);
			}else{
				data = systemService.saveMenu(menu);
				RoleMenu roleMenu = new RoleMenu();
				roleMenu.setUnique_code(IdUtils.uuid());
				roleMenu.setRole_code("1");
				roleMenu.setMenu_code(menu.getUnique_code());
				roleMenu.setCreate_time(new Date());
				roleMenu.setUpdate_time(new Date());
				data = systemService.saveRoleMenu(roleMenu);
			}
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(data);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@PostMapping(value = "/user/save")
	@ResponseBody
	public Result saveUser(HttpServletRequest request,User user) {
		user.setPoster("http://47.105.47.238/images/digital_cms_image/2018/10/22/1542854169242.jpg");
		user.setUnique_code(IdUtils.uuid());
		user.setCreate_time(new Date());
		user.setUpdate_time(new Date());
		Result result = new Result();
		try {
			boolean flag = systemService.isExitUserByName(user.getId());
			System.out.println("flag:"+flag);
			int data = 0;
			if(flag==true){
				data = systemService.updateUser(user);
			}else{
				data = systemService.saveUser(user);
				RoleUser roleUser = new RoleUser();
				roleUser.setUnique_code(IdUtils.uuid());
				roleUser.setRole_code(user.getRole_name());
				roleUser.setPartner_code(user.getUnique_code());
				roleUser.setCreate_time(new Date());
				roleUser.setUpdate_time(new Date());
				data = systemService.saveRoleUser(roleUser);
			}
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(data);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

}
